# coding:utf-8
import time
import json
from selenium import webdriver
from selenium.webdriver import ChromeOptions as Options

def lg_login(url):
    driver = webdriver.Chrome(executable_path="C:\\Users\\16491\\Downloads\\chromedriver_win32\\chromedriver.exe")
    driver.maximize_window()
    # 登录需要验证的网页
    driver.get(url)
    cookie_path = '../jobs/cookie.json'
    time.sleep(4)
    cookie_str = ''
    # qrcode
    qrcode_button = driver.find_element_by_xpath("/html/body/section/div[1]/div[1]")
    qrcode_button.click()
    while True:
        print('请在30秒之内扫码!!')
        time.sleep(30)
        try:
            input_ = driver.find_element_by_xpath('/html/body/section/div[2]/div[1]/div[1]/ul/li[1]')
            value = input_.get_attribute('value')
            if '登录' in value:
                continue
        except Exception as e:
            print(e)
            cookie_items = driver.get_cookies()
            cookie_str = ""
            for item_cookie in cookie_items:
                item_str = item_cookie["name"] + "=" + item_cookie["value"] + "; "
                cookie_str += item_str
            with open(cookie_path,'w') as f:
                json.dump({'cookie':cookie_str},f)

            return cookie_str
    return cookie_str
    # phone
    # username_elem = driver.find_element_by_xpath("/html/body/section/div[2]/div[1]/div[2]/form/div[1]/input")
    # username_elem.clear()
    # username_elem.send_keys(username)
    # password
    # password_elem = driver.find_element_by_xpath("/html/body/section/div[2]/div[1]/div[2]/form/div[2]/input")
    # password_elem.clear()
    # password_elem.send_keys(password)
    # serect
    # privacy_button = driver.find_element_by_xpath("/html/body/section/div[2]/div[3]/div[2]/img[1]")
    # privacy_button.click()
    # submit
    # login_button = driver.find_element_by_xpath("/html/body/section/div[2]/div[1]/div[2]/form/div[5]/input")
    # login_button.click()


def boss_login(url='https://www.zhipin.com/c101010100/?query=python&page=2&ka=page-2'):
    driver = webdriver.Chrome(executable_path="C:\\Users\\16491\\Downloads\\chromedriver_win32\\chromedriver.exe")
    driver.maximize_window()
    # 登录需要验证的网页
    driver.get(url)
    cookie_path = '../jobs/boss_cookie.json'
    time.sleep(4)
    cookie_items = driver.get_cookies()
    cookie_str = ""
    for item_cookie in cookie_items:
        item_str = item_cookie["name"] + "=" + item_cookie["value"] + "; "
        cookie_str += item_str
    with open(cookie_path, 'w') as f:
        json.dump({'cookie': cookie_str}, f)

    return cookie_str


def debug_chrome():

    chrome_options = Options()
    # 使用命令chrome.exe --remote-debugging-port=9222
    # 开启debug模式，映射到9222端口，控制此端口
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    # driver就是当前浏览器窗口
    driver = webdriver.Chrome(options=chrome_options, executable_path="C:\\Users\\16491\\Downloads\\chromedriver_win32\\chromedriver.exe")
    return driver